package com.my.test.jvm;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

public class Reference {

    private static Object strongRef = new Object();  // 静态变量不会被gc

    private String s = "123";  // 常量池中不会被gc


    public static void main(String[] args) {
        // 静态不会被回收
        Object obj = strongRef;
        strongRef = null;
        System.gc();
        System.out.println(obj); // 静态变量不会被回收
        System.out.println("静态变量引用的对象不会被回收");
        Object strongRef2 = new Object();
        strongRef2 = null;
        System.gc();
        System.out.println(strongRef2); // GCRoot不可达回收
        System.out.println("----------------------");
        Object softRef = new Object();
        SoftReference<Object> soft  = new SoftReference<Object>(softRef);
        softRef = null;
        System.out.println(soft.get());
        soft = null;
        //System.out.println(soft.get());
        System.out.println("----------------------");
        Object weakRef = new Object();
        WeakReference<Object> weak  = new WeakReference<Object>(weakRef);
        weakRef = null;
        System.gc();
        System.out.println(weak.get());
    }
}
